<?

//Initiate Authenticate
SecurityRedirect ();
//error_reporting(E_ALL);

//initiate Vars 
define("PRIMARY_KEY","CUDEBTUR");
$FileName      = "ais_transaction_debtur.php";
$FileTemplate  = "ais_transaction_debtur_form.html";
$SubHeaderTitle = "Debtur";
$TableName		= "tbldebtur";
$HTMLFormName   = "CustForm";

$FormAction    = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType   = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;
$FormFields    = array("CUCODE","CUNAME","CUADDR","CUZIP","CNCODE","CUPHONE","CUFAX","CUCONTACT","CUEMAIL","CUNPWP","CUSKADA","CUDEBTUR","TOP"); 
buildsqlfield ("$TableName", $FormFields);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name");
$FormCValue    = BuildHTMLField($FormFields,"value");
//$FDept 		   = GetParam("deptSearch","");

//************ FORM ACTION **************
if (GetParam("form","")==$HTMLFormName) {
	if (GetParam("Action","")=="Add") {
		FormInsert();
	}

	if (GetParam("Action","")=="Update") {
		$FormMode="Edit";
		FormUpdate();
	}

	if (GetParam("Action","")=="Delete") {
		$FormMode="Edit";
		$FormMode="Edit";
		FormDelete();
	}

	if (GetParam("Action","")=="Cancel") {
		$FormMode="Edit";
		FormCancel();
	}
}

//Field Verification 
$Verification  = "Enable";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
$VerifyFields  = array("CUCODE"=>"Customer Code","CUNAME"=>"Customer Name","CUADDR"=>"Address","CNCODE"=>"Country","TOP"=>"Term Of Payment");
VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar);

//************ SET FORM MODE ***************
// PENAMBAHAN SKRIP DISINI \\
if (GetParam(PRIMARY_KEY,"")) {
		$FormMode = "Edit";
} else {
		$FormMode = "Add";
}

Init($SessionVar,CreateVerifyLoader ($FileName,"iLoader",PRIMARY_KEY,"$HTMLFormName.".PRIMARY_KEY, $FormMode));
$InitFunction = "isExist".PRIMARY_KEY."()";

//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM $TableName";
if (GetParam(PRIMARY_KEY,"") && $FormMode="Edit") {
    $SQLForm = $SQL." WHERE ".PRIMARY_KEY."='".trim(GetParam(PRIMARY_KEY,""))."'";
}
$SQLCountry = "SELECT * FROM idms_general.tblcountry where (CNDESC is not null or CNDESC='') order by CNDESC";

//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

//HTML Pulldown
$TBS->MergeBlock("Countryblk",$DBMysql,$SQLCountry);

if (GetParam(PRIMARY_KEY,"")) $TBS->MergeBlock("blv","mysql",$SQLForm);  
else $TBS->MergeBlock("blv",$FormCValue);

//HTML Name
$TBS->MergeBlock("bln",$FormCName);
$TBS->Show();


//*************** ACTION FUNCTION *******************
function FormInsert() {
	global $DBConnection,$FileName,$TableName;
	$SQL = "
		INSERT INTO $TableName(CUCODE,CUTYPE,CUNAME,CUADDR,CUZIP,CNCODE,CUPHONE,CUFAX,CUCONTACT,CUEMAIL,CUNPWP,CUSKADA,CUDEBTUR)
		VALUES
		('$_POST[CUDEBTUR]','1000','$_POST[CUNAME]','$_POST[CUADDR]','$_POST[CUZIP]','$_POST[CNCODE]','$_POST[CUPHONE]','$_POST[CUFAX]','$_POST[CUCONTACT]','$_POST[CUEMAIL]','$_POST[CUNPWP]','$_POST[CUSKADA]','$_POST[CUDEBTUR]')
		";

	$DBConnection->dbc->query($SQL);
	header("Location: ais_transaction_custlist.php?FormName=ReceiptForm&debturCodeSearch=".$_POST[CUDEBTUR]);
	exit;
}

function FormUpdate() {
	global $DBConnection;
	$DBConnection->Update();
}

function FormDelete() {
	global $DBConnection,$FileName,$TableName;
	$DBConnection->Delete("$TableName",PRIMARY_KEY);
	header("Location: $FileName");
	exit;	
}

function FormSearch () {

}

function FormCancel () {
	global $FileName;
	header("Location: ".$FileName);
	exit;
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
		global $DBConnection;
		for ($x=0;$x<count($arrayfield);$x++) {
			$DBConnection->add_htmlfield($table,$arrayfield[$x],GetParam($arrayfield[$x],""));
		}
}

?>